package com.loong.musicdemo.ui.find;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.ui.find.activity.DailyRecommendActivity;
import com.loong.musicdemo.ui.find.activity.SearchActivity;
import com.loong.musicdemo.ui.find.adapter.PlayListAdapter;
import com.loong.musicdemo.ui.find.adapter.DailyNewSongAdapter;
import com.loong.musicdemo.util.dataDeal.MusicDeal;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindFragment extends Fragment {
    private Integer[] imgId;//存放轮播图图片的id
    private List<Integer> imgs = new ArrayList<Integer>();
    private MZBannerView<Integer> mzBannerView;
    private RecyclerView recyclerNewSong;
    private RecyclerView recyclerPlaylist;
    private ImageView search_icon;
    private LinearLayout daily_new_song;
    private List<Song> recommends = new ArrayList<>();
    private List<SongList> playlist = new ArrayList<>();
    private DailyNewSongAdapter dailyNewSongAdapter;
    private PlayListAdapter playListAdapter;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_find, container, false);
        mzBannerView = root.findViewById(R.id.banner);
        recyclerNewSong = root.findViewById(R.id.recycler_new_song);
        recyclerPlaylist = root.findViewById(R.id.recycler_playlist);
        daily_new_song = root.findViewById(R.id.daily_new_song);
        search_icon = root.findViewById(R.id.search_icon);
        imgId = new Integer[]{R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven};
        initEventHandler(root);
        initMZBannerView();
        initRecyclerView();
        new PlayListTask().execute();
        new RecommendMusicTask().execute();
        return root;
    }

    /**
     *  初始化搜索框图片与新歌速递布局的点击事件
     * @param root 根 view
     */
    private void initEventHandler(View root){
        search_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(root.getContext(), SearchActivity.class);
                startActivity(intent);
            }
        });
        daily_new_song.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(root.getContext(), DailyRecommendActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyclerView() {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL);
        recyclerNewSong.setLayoutManager(layoutManager);
        dailyNewSongAdapter = new DailyNewSongAdapter(recommends,getContext());
        recyclerNewSong.setAdapter(dailyNewSongAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerPlaylist.setLayoutManager(manager);
        playListAdapter = new PlayListAdapter(playlist,getContext());
        recyclerPlaylist.setAdapter(playListAdapter);
    }

    /**
     * 初始化轮播图
     */
    private void initMZBannerView(){
        imgs.addAll(Arrays.asList(imgId));
        mzBannerView.setPages(imgs,new MZHolderCreator<BannerImageHolder>() {
            @Override
            public BannerImageHolder createViewHolder() {
                return new BannerImageHolder();
            }
        });
        mzBannerView.setDuration(2000);
    }

    /**
     * 设置轮播图中的内容
     */
    public static class BannerImageHolder implements MZViewHolder<Integer> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            mImageView = view.findViewById(R.id.banner_image);
            return view;
        }
        @Override
        public void onBind(Context context, int position, Integer data) {
            mImageView.setImageResource(data);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        mzBannerView.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mzBannerView.start();
    }
    class RecommendMusicTask extends AsyncTask<Song,Void,List<Song>>{

        @Override
        protected void onPostExecute(List<Song> song) {
            super.onPostExecute(song);
            recommends.addAll(song);
            dailyNewSongAdapter.notifyDataSetChanged();
        }

        @Override
        protected List<Song> doInBackground(Song... songs) {
            return MusicDeal.getRecommendNewSongs();
        }
    }


    class PlayListTask extends AsyncTask<SongList,Void,List<SongList>>{

        @Override
        protected void onPostExecute(List<SongList> songLists) {
            super.onPostExecute(songLists);
            playlist.addAll(songLists);
            playListAdapter.notifyDataSetChanged();
        }

        @Override
        protected List<SongList> doInBackground(SongList... songLists) {
            return MusicDeal.getSongList();
        }
    }
}
